package vistas.paneles;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Date;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import daos.AtletaDAO;
import entidades.Atleta;
import vistas.abstractModels.AtletaTableModel;

public class Pagos extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField id;
	private JComboBox<Integer> montoPagar;
	private JButton cambiar, aceptar;
	private JScrollPane scroll1, scroll2;
	private JTable listado;
	private JTextArea descripcion;
	private AtletaDAO atletaDAO;

	public Pagos() {

		instancias();

		agregarComponentes();

		setListeners();
	}

	private void instancias() {
		atletaDAO = new AtletaDAO();
		id = new JTextField();
		id.setEditable(false);
		configurarComponente(id, "Cédula", 150);
		montoPagar = new JComboBox<Integer>();
		configurarComponente(montoPagar, "Meses a pagar", 150);
		cambiar = new JButton("Lista negra");
		aceptar = new JButton("Pagar");
		aceptar.setEnabled(false);
		listado = new JTable(new AtletaTableModel(atletaDAO.obtenListaAtleta(),
				AtletaTableModel.SIMPLE));
		scroll1 = new JScrollPane(listado);
		descripcion = new JTextArea();
		configurarComponente(descripcion, "Descripción", 400);
		descripcion.setPreferredSize(new Dimension(400, 420));
		scroll2 = new JScrollPane(descripcion);
		descripcion.setEditable(false);
	}
	
	private void configurarComponente(JComponent component, String string,
			Integer lenght) {
		component.setPreferredSize(new Dimension(lenght, 30));
		component.setBorder(BorderFactory.createTitledBorder(string));
	}

	private void agregarComponentes() {
		JPanel panel1 = new JPanel();
		GroupLayout layout = new GroupLayout(panel1);
		panel1.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		layout.setHorizontalGroup(layout
				.createSequentialGroup()
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.LEADING).addComponent(id)
								.addComponent(aceptar))
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.LEADING)
								.addComponent(montoPagar)
								.addComponent(cambiar)));

		layout.setVerticalGroup(layout
				.createSequentialGroup()
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE)
								.addComponent(id).addComponent(montoPagar))
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE)
								.addComponent(aceptar).addComponent(cambiar)));
		add(panel1);
		add(scroll1);
		add(scroll2);
	}

	private void setListeners() {

		aceptar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Atleta atleta = atletaDAO.actualizarDeudaAtleta(Integer
						.parseInt(id.getText()));
				if (montoPagar.getSelectedItem() != null) {
					atleta.setDeuda(atleta.getDeuda()
							- (Integer) montoPagar.getSelectedItem());
					atleta.setUltimoPago(new Date(new java.util.Date()
							.getTime()));
				}
				atletaDAO.actualizaAtleta(atleta);
				id.setText("");
				montoPagar.removeAllItems();
				descripcion.setText("");
			}
		});

		cambiar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
			
				if(cambiar.getText() == "Lista negra"){
					cambiar.setText("Todos");
					listado.setModel(new AtletaTableModel(atletaDAO
							.obtenListaAtleta(), AtletaTableModel.BLACKLIST));
				}
				else{
					cambiar.setText("Lista negra");
				listado.setModel(new AtletaTableModel(atletaDAO
						.obtenListaAtleta(), AtletaTableModel.SIMPLE));
				}
			}
		});

		listado.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				if (id.getText() != "")
					aceptar.setEnabled(true);
				id.setText(listado.getValueAt(listado.getSelectedRow(), 0)
						.toString());
				Atleta atleta = atletaDAO.actualizarDeudaAtleta(Integer
						.valueOf(id.getText()));
				montoPagar.removeAllItems();

				for (int i = atleta.getDeuda(); i > 0; i--) {
					montoPagar.addItem(i);
					System.out.println("los anios son iguales ");
				}

				String desc = atleta.getId().toString() + "\n"
						+ atleta.getNombres() + "\n" + atleta.getApellidos()
						+ "\n" + atleta.getDireccion() + "\n"
						+ atleta.getTelefonoA() + "\n" + "Cinturón "
						+ atleta.getCinturon() + "\n" + "Deuda "
						+ atleta.getDeuda() + " Meses";

				descripcion.setText(desc);
				if (montoPagar.getItemCount() == 0)
					aceptar.setEnabled(false);

			}
		});

		listado.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {
				aceptar.setEnabled(true);
				id.setText(listado.getValueAt(listado.getSelectedRow(), 0)
						.toString());
				Atleta atleta = atletaDAO.actualizarDeudaAtleta(Integer
						.valueOf(id.getText()));
				montoPagar.removeAllItems();
				for (int i = atleta.getDeuda(); i > 0; i--) {
					montoPagar.addItem(i);
				}
				String desc = atleta.getId().toString() + "\n"
						+ atleta.getNombres() + "\n" + atleta.getApellidos()
						+ "\n" + atleta.getDireccion() + "\n"
						+ atleta.getTelefonoA() + "\n" + "Cinturón "
						+ atleta.getCinturon() + "\n" + "Deuda "
						+ atleta.getDeuda() + " Meses";

				descripcion.setText(desc);
				if (montoPagar.getItemCount() == 0)
					aceptar.setEnabled(false);
			}

			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

		
		});

	}

}
